<script setup lang="ts">
import { computed, nextTick, reactive, watch, ref } from 'vue';
import { useAntdForm, useFormRules } from '@/hooks/common/form';
import {farmCreateApi, farmEditApi, getFarmLabelListApi} from '@/service/api/businessApi';
import { useProjectStore } from '@/store/modules/project';
const projectStore = useProjectStore();
defineOptions({
  name: 'GetewayOperateDrawer'
});

interface Props {
  operateType: AntDesign.TableOperateType;
  rowData?: any | null;
}

const props = defineProps<Props>();

interface Emits {
  (e: 'submitted'): void;
}

const emit = defineEmits<Emits>();

const visible = defineModel<boolean>('visible', {
  default: false
});

const farmLabelList = defineModel<any>('farmLabelList', {
  default: () => []
});

const { formRef, validate, resetFields } = useAntdForm();
const { defaultRequiredRule } = useFormRules();

const title = computed(() => {
  const titles: Record<AntDesign.TableOperateType, string> = {
    add: '新增养殖池',
    edit: '编辑养殖池'
  };
  return titles[props.operateType];
});

const model: any = reactive(createDefaultModel());

function createDefaultModel(): any {
  return {};
}

const rules: any = {
  name: defaultRequiredRule,
  sort: defaultRequiredRule,
  lableId: defaultRequiredRule
};

/** the enabled role options */
async function handleInitModel() {
  Object.assign(model, createDefaultModel());

  if (props.operateType === 'edit' && props.rowData) {
    await nextTick();
    Object.assign(model, props.rowData);
  }
}

function closeDrawer() {
  visible.value = false;
}

// eslint-disable-next-line no-warning-comments
/* TODO 设备编码不填写不行， 后面要删掉 */

const doAddSubmit = async () => {
  const res = await farmCreateApi({
    ...model,
    projectId: projectStore.currentProject?.id
  });
  if (res.error === null) {
    window.$message?.success('新增成功');
    closeDrawer();
    emit('submitted');
  }
};
const doEditSubmit = async () => {
  const res = await farmEditApi({
    ...model
  });
  if (res.error === null) {
    window.$message?.success('更新成功');
    closeDrawer();
    emit('submitted');
  }
};

async function handleSubmit() {
  await validate();
  if (props.operateType === 'add') {
    await doAddSubmit();
  }
  if (props.operateType === 'edit') {
    await doEditSubmit();
  }
}

watch(visible, () => {
  if (visible.value) {
    handleInitModel();
    resetFields();
  }
});
</script>

<template>
  <AModal v-model:open="visible" :title="title" :width="560" :mask-closable="false">
    <AForm ref="formRef" layout="vertical" :model="model" :rules="rules">
      <AFormItem label="养殖池名称" name="name">
        <AInput v-model:value="model.name" placeholder="请输入养殖池名称" />
      </AFormItem>
      <AFormItem label="养殖品种" name="sort">
        <AInput v-model:value="model.sort" placeholder="请输入养殖品种" />
      </AFormItem>
      <AFormItem label="分类" name="labelId">
        <ASelect v-model:value="model.labelId" placeholder="请选择分类">
          <ASelectOption v-for="item in farmLabelList" :key="item.id">
            {{ item.label }}
          </ASelectOption>
        </ASelect>
      </AFormItem>
      <AFormItem label="序号" name="sequence">
        <AInputNumber
          v-model:value="model.sequence"
          :min="1"
          class="w-full"
          placeholder="请输入序号, 序号越小列表越靠前"
        />
      </AFormItem>
      <AFormItem label="备注" name="remark">
        <ATextarea v-model:value="model.remark" placeholder="请输入备注" />
      </AFormItem>
    </AForm>
    <template #footer>
      <ASpace :size="16">
        <AButton @click="closeDrawer">取消</AButton>
        <AButton type="primary" @click="handleSubmit">确认</AButton>
      </ASpace>
    </template>
  </AModal>
</template>

<style scoped></style>
